package com.mdxx.xczd.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.mdxx.xczd.db.helper.ServerDBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ServerDAO implements IServerDAO {
	public ServerDBHelper dbHelper;

	public ServerDAO(Context context) {
		dbHelper = new ServerDBHelper(context);
	}

	@Override
	public boolean add(String table, ContentValues values) {
		boolean flag = false;
		SQLiteDatabase db = null;
		try {
			db = dbHelper.getWritableDatabase();
			long id = db.insert(table, null, values);
			flag = (id != -1 ? true : false);
		} catch (Exception e) {			
			e.printStackTrace();			
		} finally {
			if (null != db) {
				db.close();
			}
		}
		
		return flag;
	}

	@Override
	public boolean add(String sql) {
		return false;
	}

	@Override
	public List<Map<String, String>> queryMulti(String table, String[] columns, String whereClause, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		SQLiteDatabase db = null;
		Cursor cursor;
		try {
			db = dbHelper.getWritableDatabase();
			cursor = db.query(table, columns, whereClause, selectionArgs, groupBy, having, orderBy, limit);
			while (cursor.moveToNext()) {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < cursor.getColumnCount(); i++) {
					String columnName = cursor.getColumnName(i);
					String columnValue = cursor.getString(cursor.getColumnIndex(columnName));
					map.put(columnName, columnValue);
				}
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != db) {
				db.close();
			}
		}
		return list;
	}

	@Override
	public String queryValue(String table, String[] columns, String key, String value) {
		String result = null;
		SQLiteDatabase db = null;
		try {
			db = dbHelper.getWritableDatabase();
			Cursor cursor = db.query(table, columns, key + " like ?", new String[]{value}, null, null, null, null);
			cursor.moveToNext();
			String columnName = cursor.getColumnName(0);
			result = cursor.getString(cursor.getColumnIndex(columnName));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != db) {
				db.close();
			}
		}
		return result;
	}

	@Override
	public String queryValue(String sql) {
		return null;
	}

	@Override
	public <T> List<T> queryMulti(String sql) {
		return null;
	}

	@Override
	public boolean delete(String sql) {
		SQLiteDatabase db = null;
		try {
			db = dbHelper.getWritableDatabase();
			db.execSQL(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(null != db){
				db.close();
			}
		}
		return false;
	}
	
	@Override
	public boolean update(String sql) {
		SQLiteDatabase db = null;
		try {
			db = dbHelper.getWritableDatabase();
			db.execSQL(sql);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(null != db){
				db.close();
			}
		}
		return false;
	}

	@Override
	public boolean delete(String table, String key, String value) {
		return false;
	}

	@Override
	public String querySingle(String sql) {
		return null;
	}
}
